/**
 * 
 */
package br.com.livraria.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.livraria.dao.AbstractDao;
import br.com.livraria.dao.AutorDao;
import br.com.livraria.vo.Autor;

/**
 * @author Jeferson
 *
 */
public class AutorDaoImpl extends AbstractDao implements AutorDao {

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.AutorDao#incluir(br.com.livraria.vo.Autor)
	 */
	@Override
	public void incluir(Autor autor) {
		
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement =
					con.prepareStatement("INSERT INTO autor (nome) VALUES (?)");
			
			preparedStatement.setString(1, autor.getNome());
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}

	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.AutorDao#alterar(br.com.livraria.vo.Autor)
	 */
	@Override
	public void alterar(Autor autor) {
	
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement =
					con.prepareStatement(" UPDATE autor SET nome = ?  WHERE id = ? ");
			
			preparedStatement.setString(1, autor.getNome());
			preparedStatement.setInt(2, autor.getId());
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		
	

	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.AutorDao#excluir(java.lang.Integer)
	 */
	@Override
	public void excluir(Integer id) {
		try {
			Connection con = open();
			
			PreparedStatement preparedStatement =
					con.prepareStatement(" DELETE FROM autor WHERE id = ? ");
			
			preparedStatement.setInt(1, id);
			
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.AutorDao#consultarPorId(java.lang.Integer)
	 */
	@Override
	public Autor consultarPorId(Integer id) {
		Autor autor = new Autor();
		try {
			Connection con = open();
			PreparedStatement pst = con.prepareStatement("SELECT * FROM autor WHERE id = ?");
			
			pst.setInt(1, id);
					
			ResultSet rs = pst.executeQuery();
			
			while (rs.next()) {
				autor.setId(rs.getInt("id"));
				autor.setNome(rs.getString("nome"));				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return autor;
	}

	/* (non-Javadoc)
	 * @see br.com.livraria.dao.AutorDao#consultarPorNome(java.lang.String)
	 */
	@Override
	public List<Autor> consultarPorNome(String nome) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Autor> listarTodos() {
		List<Autor> listaAutores = new ArrayList<Autor>();
		try {
			Connection con = open();
			Statement statement = con.createStatement();
			ResultSet rs = statement.executeQuery("SELECT * FROM autor");
			
			while (rs.next()) {
				Autor a = new Autor();
				a.setId(rs.getInt("id"));
				a.setNome(rs.getString("nome"));
				listaAutores.add(a);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return listaAutores;
	}

	@Override
	public Autor consultarPorEmail(String email) {
		Autor autor = null;
		try {
			Connection con = open();
			PreparedStatement pstmt= con.prepareStatement("SELECT * FROM autor WHERE email = ? ");
			
			pstmt.setString(1, email);
			
			ResultSet rs = pstmt.executeQuery();
			
			if(rs.next()) {
				autor = new Autor();
				autor.setId(rs.getInt("id"));
				autor.setNome(rs.getString("nome"));
				autor.setEmail(rs.getString("email"));
				autor.setTelefone(rs.getLong("Telefone"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return autor;
	}

}
